package org.jeecg.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.asset.entity.AssetLedger;
import org.jeecg.modules.asset.entity.AssetMessage;
import org.jeecg.modules.asset.service.IAssetLedgerService;
import org.jeecg.modules.asset.service.IAssetMessageService;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @Description: 后台首页统计
* @Date:   2025-11-02
* @Version: V1.0
*/
@Api(tags="后台首页统计")
@RestController
@RequestMapping("/asset/indexTotal")
@Slf4j
public class indexController extends JeecgController<AssetLedger, IAssetLedgerService> {
   @Autowired
   private IAssetMessageService assetMessageService;
   @Autowired
   private ISysUserService sysUserService;
   @Autowired
   private ISysRoleService sysRoleService;
   @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 资产台账统计
     */
    @ApiOperation(value="资产台账统计", notes="资产台账统计")
    @GetMapping(value = "/indexTotal")
    public Result<Map<String, Object>> indexTotal() {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<AssetMessage> queryWrapper = new QueryWrapper<>();
        // 总资产数量
        List<AssetMessage> assetList = assetMessageService.list(queryWrapper);
        map.put("assetTotal", assetList.size());
        // 运营中资产
        // 自用	1
        // 出租	2
        // 运营中  11
        queryWrapper.clear();
        long useAsset = assetMessageService.count(queryWrapper.in("status", "1,2,11".split(",")));
        map.put("useAssetTotal", useAsset);
        // 使用率
        if (assetList.size() != 0){
            map.put("useRate", new DecimalFormat("0.0").format(useAsset * 100 / assetList.size()));
        }else {
            map.put("useRate", 0);
        }
        // 闲置资产
        queryWrapper.clear();
        long leaveNum = assetMessageService.count(queryWrapper.in("status", "3,12".split(",")));
        map.put("leaveAssetTotal", leaveNum);
        // 总建筑面积 获取 status为1（运营状态），floorArea求和
        double allFloorArea = assetList.stream()
                .filter(asset -> asset.getBuildingArea() != null)
                .mapToDouble(asset -> Double.parseDouble(asset.getBuildingArea().toString()))
                .sum();
        map.put("floorArea", allFloorArea);
        return Result.OK(map);
    }

    /**
     * 用户统计
     */
    @ApiOperation(value="用户统计", notes="用户统计")
    @GetMapping(value = "/userTotal")
    public Result<List<Map<String, Object>>> userTotal() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<SysUser> userList = sysUserService.list();
        //正常状态
        long normalUser = userList.stream().filter(user -> user.getStatus() == 1).count();
        //禁用状态
        long disableUser = userList.stream().filter(user -> user.getStatus() == 2).count();
        Map<String, Object> normalMap = new HashMap<>();
        Map<String, Object> allMap = new HashMap<>();
        Map<String, Object> disableMap = new HashMap<>();
        allMap.put("x", "总计");
        allMap.put("y", userList.size());
        normalMap.put("x", "激活");
        normalMap.put("y", normalUser);
        disableMap.put("x", "禁用");
        disableMap.put("y", disableUser);
        list.add(allMap);
        list.add(normalMap);
        list.add(disableMap);
        return Result.OK(list);
    }

    /**
     * 角色统计
     */
    @ApiOperation(value="角色统计", notes="角色统计")
    @GetMapping(value = "/roleTotal")
    public Result<List<Map<String, Object>>> roleTotal() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<SysRole> roleList = sysRoleService.list();
        for (SysRole role : roleList){
            Map<String, Object> map = new HashMap<>();
            Page<SysUser> page = new Page<>(1, 1);
            IPage<SysUser> userList = sysUserService.getUserByRoleId(page, role.getId(), null);
            map.put("x", role.getRoleName());
            map.put("y", userList.getTotal());
            list.add(map);
        }
        return Result.OK(list);
    }
}
